package cn.tedu.service;

import cn.tedu.dao.AddressMapper;
import cn.tedu.entity.Address;
import cn.tedu.service.ex.PhoneExcption;
import cn.tedu.service.ex.SiteNotFoundExcption;
import cn.tedu.service.ex.UserNotFoundException;
import cn.tedu.service.port.AddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("/addressService")
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;

    /**
     * 新增收货地址
     * @param address   信息对象
     * @return  成功返回插入地址的id
     */
    public Integer add(Address address) {
        Address address1 = new Address();
        address1.setUid(address.getUid());
        if (address.getRecvName() == null || address.getRecvName().trim().isEmpty()){
            throw new UserNotFoundException("收货人不能为空");
        }
        address1.setRecvName(address.getRecvName());
        if (address.getRecvProvince() == null || address.getRecvProvince().trim().isEmpty()){
            throw new SiteNotFoundExcption("省份不能为空");
        }
        address1.setRecvProvince(address.getRecvProvince());
        if (address.getRecvCity() == null || address.getRecvCity().trim().isEmpty()){
            throw new SiteNotFoundExcption("城市不能为空");
        }
        address1.setRecvCity(address.getRecvCity());
        if (address.getRecvArea() == null || address.getRecvArea().trim().isEmpty()){
            throw new SiteNotFoundExcption("区/县不能为空");
        }
        address1.setRecvArea(address.getRecvArea());
        if (address.getRecvAddr() == null || address.getRecvAddr().trim().isEmpty()){
            throw new SiteNotFoundExcption("详细地址不能为空");
        }
        address1.setRecvAddr(address.getRecvAddr());
        if (address.getRecvPhone() == null || address.getRecvPhone().trim().isEmpty()){
            throw new PhoneExcption("手机号码不能为空");
        }
        address1.setRecvPhone(address.getRecvPhone());
        address1.setRecvTel(address.getRecvTel());
        address1.setRecvZip(address.getRecvZip());
        address1.setRecvTag(address.getRecvTag());
        String Province = addressMapper.selectProvinceByCode(address1.getRecvProvince());
        String City = addressMapper.selectCityByCode(address1.getRecvCity());
        String Area = addressMapper.selectAreaByCode(address1.getRecvArea());
        //判断IsDefault 属性是否是空值，如果为则将设为默认1，如果不为空则为0
        //根据IsDefault 会默认设置地址信息
        List<Address> list = addressMapper.select(address.getUid());
        if (list.size() == 0){
            address1.setIsDefault(1);
        }else {
            address1.setIsDefault(0);
        }

        address1.setRecvDistrict(Province + City + Area);
        address1.setCreatedTime(new Date());
        Integer a = addressMapper.insert(address1);
        return a;
    }

    /**
     * 根据uid查询地址信息
     * @param uid   uid信息
     * @return  成功返回address地址集合
     * @throws SiteNotFoundExcption 自定义地址信息异常
     */
    public List<Address> selectAddress(Integer uid){
        List<Address> list = addressMapper.select(uid);
        return list;
    }

    /**
     * 根据id 删除对应地址信息,如果删除的信息为当前默认地址，则选中
     * 其剩余信息中的一条设为默认地址
     * @param id    address地址信息id
     */
    public void deleteAddress(Integer id, Integer uid) throws SiteNotFoundExcption{
        Address address = addressMapper.selectAddressByIdAndUid(id, uid);
        if (address == null){
            throw new SiteNotFoundExcption("该地址信息不存在，请刷新页面或重新登录");
        }
        addressMapper.delete(id, uid);

        if (address.getIsDefault() == 1){
            List<Address> addresses = addressMapper.select(uid);
            //判断集合长度是否大于0
            if (addresses.size() > 0){
                //大于0
                //即删除后还有收货地址的数据
                //则取出第1条数据的id
                Integer newDefaultId = addresses.get(0).getId();
                //将其设为默认收货地址
                Integer a = setDefault(uid, newDefaultId);
                if (a == 0){
                    throw new SiteNotFoundExcption("设置默认失败，请重新登录");
                }
            }else {
                //长度为0
                //即删除后没有收货地址数据了
                //则不需要执行任何操作
            }
        }
    }

    /**
     * 根据id 修改对应地址信息
     * @param address   地址信息对象
     * @return  返回受影响行数
     */
    public Integer updateAddress(Address address,Integer id, Integer uid) {
        String Province = addressMapper.selectProvinceByCode(address.getRecvProvince());
        String City = addressMapper.selectCityByCode(address.getRecvCity());
        String Area = addressMapper.selectAreaByCode(address.getRecvArea());
        address.setRecvDistrict(Province + City + Area);
        address.setId(id);
        address.setUid(uid);
        Integer a = addressMapper.update(address);
        return a;
    }

    /**
     * 根据id查询address地址对象
     * @param id    用户地址id
     * @param uid   用户id
     * @return  返回address地址对象
     */
    public Address getAddressByIdAndUid(Integer id, Integer uid) {
        Address address = addressMapper.selectAddressByIdAndUid(id,uid);
        return address;
    }

    /**
     * 根据用户uid和该地址id将选中地址状态设置为默认，并将未选中地址取消默认状态
     * @param uid   用户id
     * @param id    选中地址id
     * @return  成功修改返回1，失败返回0
     */
    @Transactional
    public Integer setDefault(Integer uid, Integer id) {
        Integer a = addressMapper.cancelAllDefault(uid);
        if (a == 0){
            throw new SiteNotFoundExcption("查询不到用户地址信息,请重新登录尝试");
        }
        return addressMapper.setDefaultByIdAndUid(uid, id);
    }


}
